package com.parkcar;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import HttpUtils.HttpInfoRequest;
import Modle.InfoBean;
import ui.SharePop;
import util.BaseActivity;
import util.BaseTools;
import util.BaseUtil;
import util.Constrants;

/**
 * Created by Administrator on 2016/1/14.
 */
public class HomeMineActivity extends BaseActivity {
    private TextView count_tv;
    private ImageView header_iv;
    private TextView user_name;
    private HttpInfoRequest mInfoRequest,loginOutRequest;
    private InfoBean mInfo;
    private ReChargeListener reChargeListener;

    @Override
    protected void setUpui(Bundle arg0) {
        setContentView(R.layout.activity_home_mine);
        setTitleGone(View.GONE);
        count_tv = (TextView) findViewById(R.id.count_tv);
        header_iv = (ImageView) findViewById(R.id.header_iv);
        user_name = (TextView) findViewById(R.id.user_name);
      /*  btLoginOut = (Button) findViewById(R.id.btLoginOut);
        btLoginOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginout();
            }
        });*/
        if (BaseUtil.isLogin(this)) {
            getData();
        }
        reChargeListener = new ReChargeListener();
        registerReceiver(reChargeListener, new IntentFilter(Constrants.RECHARG_SUCCESS));
    }

    @Override
    protected void updateUI(boolean isLogin) {
        if (!isLogin) {
            header_iv.setBackgroundResource(R.drawable.head_img);
            user_name.setText("点击登录");
            count_tv.setText("￥0.00");
        }else{
            getData();
        }
    }

    /**
     * 充值
     *
     * @param view
     */
    public void toRecharge(View view) {
        if (!BaseUtil.isLogin(this)) {
            toast("请先登录");
            showActivity(LoginActivity.class, null);
        } else {
            showActivity(RechargeActivity.class, null);
        }
    }

    /**
     * 优惠券
     *
     * @param view
     */
    public void toCoupon(View view) {
        if (!BaseUtil.isLogin(this)) {
            toast("请先登录");
            showActivity(LoginActivity.class, null);
        } else {
            showActivity(CouponActivity.class, null);
        }
    }

    /**
     * 停车记录
     *
     * @param view
     */
    public void toHistory(View view) {
        if (!BaseUtil.isLogin(this)) {
            toast("请先登录");
            showActivity(LoginActivity.class, null);
        } else {
            showActivity(HistoryActivity.class, null);
        }
    }

    /**
     * 车辆管理
     *
     * @param view
     */
    public void carManager(View view) {
        if (!BaseUtil.isLogin(this)) {
            toast("请先登录");
            showActivity(LoginActivity.class, null);
        } else {
            showActivity(CarActivity.class, null);
        }
    }

    /**
     * 社区服务
     *
     * @param view
     */
    public void sceneServer(View view) {
        showActivity(CommunityActivity.class, null);
    }

    /**
     * 分享给好友
     *
     * @param view
     */
    public void share(View view) {
        Bundle mBundle = new Bundle();
        showActivity(SharePop.class,mBundle);
    }

    /**
     * 关于我们
     *
     * @param view
     */
    public void aboutUs(View view) {
        showActivity(AboutActivity.class, null);
    }

    /**
     * 个人中心
     *
     * @param view
     */
    public void toInfo(View view) {
        if (!BaseUtil.isLogin(this)) {
            toast("请先登录");
            showActivity(LoginActivity.class, null);
        } else {
            if (mInfo == null) {
                toast("未获取到个人信息");
            }
            showActivity(InfoActivity.class, null, Constrants.CHANGE_HEAD);
        }
    }

    /**
     * 获取数据
     */
    private void getData() {
        mInfoRequest = new HttpInfoRequest(this, 2);
        mInfoRequest.setmIHttpClinetListener(this);
        mInfoRequest.doRequest();
    }

    /**
     * 数据加载
     */
    private void initData() {
        if(BaseUtil.isLogin(this)) {
            if (Constrants.mInfo != null) {
                mInfo = Constrants.mInfo;
                if (!BaseTools.isValueEmpty(mInfo.getHeadShotUrl())) {
                    Log.e("url",mInfo.getHeadShotUrl());
                    ImageLoader.getInstance().displayImage(mInfo.getHeadShotUrl(), header_iv);
                } else {
                    header_iv.setImageResource(R.drawable.head_img);
                }
                user_name.setText(BaseTools.isValueEmpty(mInfo.getNickName()) ? "点击设置" : mInfo.getNickName());
                count_tv.setText(BaseTools.isValueEmpty(mInfo.getBalance()) ? "￥0.00" : "￥" + mInfo.getBalance());
            }
        }
    }

    @Override
    public void httpSuccessListener(Bundle successMsg) {
        super.httpSuccessListener(successMsg);
        if (mInfoRequest != null && action.equalsIgnoreCase(mInfoRequest.getAction())) {
            if (mBaseResult != null && mBaseResult.getStatus().getRetCode() == 0) {
                mInfo = mBaseResult.toObject(InfoBean.class);
                if (mInfo != null) {
                    Constrants.mInfo = mInfo;
                    initData();
                }
            } else {
                toast(mBaseResult.getStatus().getMsg());
            }

        }else if(loginOutRequest!=null&&action.equalsIgnoreCase(loginOutRequest.getAction())){
            if (mBaseResult != null && mBaseResult.getStatus().getRetCode() == 0) {
                    Constrants.mInfo = new InfoBean();
                 /*   btLoginOut.setVisibility(View.GONE);*/
                    BaseUtil.setToken(HomeMineActivity.this, "");
                   /* Intent mIntent = new Intent();
                    mIntent.setAction(Constrants.RECHARG_SUCCESS);
                    mIntent.putExtra("login", false);
                    sendBroadcast(mIntent);*/
                    toast(mBaseResult.getStatus().getMsg());
            } else{
                toast("与服务器失去连接，请稍后重试");
            }

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constrants.CHANGE_HEAD && resultCode == Constrants.CHANGE_HEAD) {
            initData();
        }

    }

    class ReChargeListener extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            getData();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(reChargeListener);
    }

}
